package controllers.participant;

import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.EventService;
import services.RegistrationService;
import controllers.AbstractController;
import domain.Event;
import domain.Registration;

@Controller
@RequestMapping("/registration/participant")
public class RegistrationParticipantController extends AbstractController {

	@Autowired
	private RegistrationService registrationService;
	@Autowired
	private EventService eventService;

	public RegistrationParticipantController() {
		super();
	}

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create(@RequestParam int eventId) {
		ModelAndView result;
		Event evento = eventService.findOne(eventId);
		Registration registration = registrationService.create(evento);
		registrationService.save(registration);
		result = new ModelAndView("welcome/index");
		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int registrationId) {
		ModelAndView result;
		Registration registration;
		Date currentDate= new Date();
		registration = registrationService.findOne(registrationId);
		Event evento = registration.getEvent();
		Boolean started = currentDate.compareTo(evento.getStartMoment()) >= 1;
		Boolean finished =currentDate.compareTo(evento.getFinishMoment())>=	1;
		result = new ModelAndView("registration/edit");
		result.addObject("isNew", false);
		result.addObject("registration", registration);
		result.addObject("started", started);
		result.addObject("finished", finished);		
		result.addObject("requestURI", "registration/participant/edit.do");

		return result;

	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Registration registration,
			BindingResult binding) {
		ModelAndView result;

		if (binding.hasErrors()) {
			result = createEditModelAndView(registration);
		} else {
			try {
				registrationService.save(registration);
				result = new ModelAndView("welcome/index");
			} catch (Throwable oops) {
				result = createEditModelAndView(registration,
						"registration.commit.error");
			}
		}

		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "delete")
	public ModelAndView delete(Registration registration, BindingResult binding) {
		ModelAndView result;

		try {
			registrationService.delete(registration);
			result = new ModelAndView("redirect:../..");
		} catch (Throwable oops) {
			result = createEditModelAndView(registration,
					"registration.commit.error");
		}
		return result;

	}

	protected ModelAndView createEditModelAndView(Registration registration) {
		ModelAndView result;
		result = createEditModelAndView(registration, null);
		return result;

	}

	protected ModelAndView createEditModelAndView(Registration registration,
			String message) {
		ModelAndView result;

		result = new ModelAndView("registration/edit");
		result.addObject("registration", registration);
		result.addObject("message", message);

		return result;

	}

}
